package Core.Controls;

import java.util.ArrayList;
import java.util.List;

import com.example.sv2.R;

import android.graphics.Bitmap;
import android.view.MotionEvent;
import Core.AGEngine;

public class AGTextButton extends AGControl {

	private List<IAGEventHandler> _clickEventHandlerList;
	
	public int _imgResid;
	private Bitmap _bitmap;
	private String _text;
	
	public AGTextButton(AGEngine engine, String text, int imgResid)
	{
		this._clickEventHandlerList = new ArrayList<IAGEventHandler>();
	
		this._imgResid = imgResid;
		this._bitmap = engine.resource.getImage(this._imgResid);
		
		this.width = this._bitmap.getWidth();
		this.height = this._bitmap.getHeight();
		this._text = text;
	}
	
	@Override
	public void onUpdate(AGEngine engine) {
		// TODO Auto-generated method stub
		if(engine.input.Action == MotionEvent.ACTION_DOWN){
			if(this.rect.contains(engine.input.X, engine.input.Y)){
				int soundid = engine.resource.getSound(R.raw.btnclick);
				engine.sound.play(soundid);
				for(int eventIndex = 0; eventIndex < this._clickEventHandlerList.size(); eventIndex++){
					this._clickEventHandlerList.get(eventIndex).onHandle(this);
				}
				engine.input.Action = MotionEvent.ACTION_UP;
			}
		}
	}

	@Override
	public void onRender(AGEngine engine) {
		// TODO Auto-generated method stub
		
		engine.graphics.drawBitmap(this.rect, this._bitmap, null);
		engine.graphics.drawText(this._text,
				this.rect,
				engine.graphics.GameTextPaint);
	}
	
	public void addClickEvent(IAGEventHandler eventHandler){
		_clickEventHandlerList.add(eventHandler);
	}

}
